package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_031A;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_031B;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *@Description: 月度绩效推送SAP
 *@Author: Dusongsong
 *@Date: 2019/12/24
 */
public class WeaHRydjxtsAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        RecordSet rs4 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用: WeaHRydjxtsAction");
        //String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String workflowid = requestInfo.getWorkflowid();
        String STATE = "2";
        List<Map<String, String>> list = new ArrayList<>();
        List<Map<String, String>> list1 = new ArrayList<>();
        ZFM_HR_031A hr_031A = new ZFM_HR_031A();
        ZFM_HR_031B hr031B = new ZFM_HR_031B();
        //查询主表
        rs.executeQuery("select b.tablename from workflow_base a,workflow_bill b where  b.id=a.formid  and a.id =?", workflowid);
        rs.next();
        String tablename = Util.null2String(rs.getString("tablename"));//表名
        baseBean.writeLog("表名" + tablename);
        rs1.execute("select * from " + tablename + " where requestid=" + requestid);
        baseBean.writeLog("查询主表:" + "select * from " + tablename + " where requestid=" + requestid);
        rs1.next();
        String mainid = Util.null2String(rs1.getString("id"));//外键
        String sqr = Util.null2String(rs1.getString("sqr"));//申请人
        String GJAHR = Util.null2String(rs1.getString("GJAHR"));//年度
        String MONAT = Util.null2String(rs1.getString("MONAT"));//月度
        String BMBM = Util.null2String(rs1.getString("BMBM"));//部门编码
        String WERKS = Util.null2String(rs1.getString("WERKS"));//人事范围
        String NUMID1 = Util.null2String(rs1.getString("NUMID"));//唯一标识
        baseBean.writeLog("NUMID1="+NUMID1);
        if ("8004".equals(WERKS) || "8007".equals(WERKS) || "ZB01".equals(WERKS)) {
            rs4.executeQuery(" select distinct case when b.field7='ZB01' OR b.field7='8004' or b.field7='8007' then b.field2 when b.field7 not in('ZB01','8004','8007') then c.departmentcode end departmentcode from hrmresource a left join cus_fielddata b on a.id=b.id left join hrmdepartment c on a.departmentid=c.id where a.id='"+sqr+"'");
            rs4.next();
            String sqrid = Util.null2String(rs4.getString("departmentcode"));//部门编码
            baseBean.writeLog("部门编码"+sqrid);
            try {
                //主表
                String BMBM_T = Util.null2String(rs1.getString("BMBM_T"));//部门编码名称
                String BMSBJBXY = Util.null2String(rs1.getString("BMSBJBXY"));//部门上报加班效益工资
                String BMSBJBTC = Util.null2String(rs1.getString("BMSBJBTC"));//部门上报加班提成工资
                String BMSBSK = Util.null2String(rs1.getString("BMSBSK"));//部门上报收款提成
                String BMSBZZSKJ = Util.null2String(rs1.getString("BMSBZZSKJ"));//部门上报自助收款机提成
                String BMSBJL = Util.null2String(rs1.getString("BMSBJL"));//部门上报激励奖金
                String BMGDJBXY = Util.null2String(rs1.getString("BMGDJBXY"));//部门规定加班效益工资
                String BMGDJBTC = Util.null2String(rs1.getString("BMGDJBTC"));//部门规定加班提成工资
                String BMGDSK1 = Util.null2String(rs1.getString("BMGDSK"));//部门规定收款提成
                String BMGDZZSKJ = Util.null2String(rs1.getString("BMGDZZSKJ"));//部门规定自助收款机提成
                String BMGDJL = Util.null2String(rs1.getString("BMGDJL"));//部门规定激励奖金
                Map<String, String> map1 = new HashMap();
                map1.put("NUMID", NUMID1);
                map1.put("GJAHR", GJAHR);
                map1.put("MONAT", MONAT);
                map1.put("BMBM", sqrid);
                map1.put("BMBM_T", BMBM_T);
                map1.put("BMSBJBXY", BMSBJBXY);
                map1.put("BMSBJBTC", BMSBJBTC);
                map1.put("BMSBSK", BMSBSK);
                map1.put("BMSBZZSKJ", BMSBZZSKJ);
                map1.put("BMSBJL", BMSBJL);
                map1.put("BMGDJBXY", BMGDJBXY);
                map1.put("BMGDJBTC", BMGDJBTC);
                map1.put("BMGDSK", BMGDSK1);
                map1.put("BMGDZZSKJ", BMGDZZSKJ);
                map1.put("BMGDJL", BMGDJL);
                baseBean.writeLog("汇总表参数:" + map1.toString());
                //汇总表明细
                String sqlhz = "select d.* from uf_jxxfhz t left join uf_jxxfhz_dt1 d on t.id=d.mainid and t.numid = d.numid  where d.numid='" + NUMID1 + "'";
                rs3.executeQuery(sqlhz);
                baseBean.writeLog(sqlhz);
                while (rs3.next()) {
                    String GZ = Util.null2String(rs3.getString("GZ"));
                    String GZ_T = Util.null2String(rs3.getString("GZ_T"));
                    String XYJE = Util.null2String(rs3.getString("XYJE"));
                    Map<String, String> map3 = new HashMap<String, String>();
                    map3.put("NUMID", NUMID1);
                    map3.put("GZ", GZ);
                    map3.put("GZ_T", GZ_T);
                    map3.put("XYJE", XYJE);
                    baseBean.writeLog("汇总表明细参数:" + map3.toString());
                    list.add(map3);
                }
                baseBean.writeLog("list相关参数:" + list.toString());
                //明细表
                String sqlmx = "select numid,pernr,jbxygz,jbtcgz,bmgdsk,bmgdsk1,zzskjtc,jljj  from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "' and monat='" + MONAT + "' and bmbm='" + BMBM + "'";
                rs2.executeQuery(sqlmx);
                while (rs2.next()) {
                    String NUMID = Util.null2String(rs2.getString("NUMID"));         //唯一标识
                    String PERNR = Util.null2String(rs2.getString("PERNR"));       //人员编码
                    String JBXYGZ = Util.null2String(rs2.getString("JBXYGZ"));         //加班效益工资
                    String JBTCGZ = Util.null2String(rs2.getString("JBTCGZ"));         //加班提成工资
                    String BMGDSK = Util.null2String(rs2.getString("BMGDSK"));         //部门规定收款提成
                    String SKTC = Util.null2String(rs2.getString("BMGDSK1"));         //收款提成
                    String ZZSKJTC = Util.null2String(rs2.getString("ZZSKJTC"));         //自助收款机提成
                    String JLJJ = Util.null2String(rs2.getString("JLJJ"));             //激励奖金
                    Map<String, String> map2 = new HashMap();
                    map2.put("GJAHR", GJAHR);
                    map2.put("MONAT", MONAT);
                    map2.put("PERNR", PERNR);
                    map2.put("JBXYGZ", JBXYGZ);
                    map2.put("JBTCGZ", JBTCGZ);
                    map2.put("BMGDSK", BMGDSK);
                    map2.put("SKTC", SKTC);
                    map2.put("ZZSKJTC", ZZSKJTC);
                    map2.put("JLJJ", JLJJ);
                    list1.add(map2);
                    baseBean.writeLog("明细表相关参数:" + map2.toString());
                }
                //调用接口
                Map resultMap = hr031B.execute(list1);
                String flag2 = (String) resultMap.get("flag");
                String message2 = (String) resultMap.get("message");
                baseBean.writeLog("ZFM_HR_031B接口返回结果——flag:" + flag2 + ";message:" + message2);
                if ("s".equalsIgnoreCase(flag2)) {
                    rs2.executeQuery(sqlmx);
                    while (rs2.next()) {
                        String NUMID = Util.null2String(rs2.getString("NUMID"));         //唯一标识
                        String JBXYGZ = Util.null2String(rs2.getString("JBXYGZ"));         //加班效益工资
                        String JBTCGZ = Util.null2String(rs2.getString("JBTCGZ"));         //加班提成工资
                        String BMGDSK = Util.null2String(rs2.getString("BMGDSK"));         //部门收款提成
                        String SKTC = Util.null2String(rs2.getString("BMGDSK1"));         //收款提成
                        String ZZSKJTC = Util.null2String(rs2.getString("ZZSKJTC"));         //自助收款机提成
                        String JLJJ = Util.null2String(rs2.getString("JLJJ"));             //激励奖金
                        String updateSQL2 = "update uf_jxxfmx set jbxygz='" + JBXYGZ + "' ,jbtcgz= '" + JBTCGZ + "', bmgdsk='" + BMGDSK + "',bmgdsk1='" + SKTC + "',zzskjtc='" + ZZSKJTC + "', jljj='" + JLJJ + "' where numid='" + NUMID + "'";
                        rs2.execute(updateSQL2);
                        baseBean.writeLog(updateSQL2);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message2);
                    return Action.FAILURE_AND_CONTINUE;
                }
                //调用接口
                Map resultMap1 = hr_031A.execute(map1,list);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_jxxfhz set bmsbjbxy='" + BMSBJBXY + "', bmsbjbtc='" + BMSBJBTC + "', bmsbsk='" +
                            BMSBSK + "',bmsbzzskj='" + BMSBZZSKJ + "',bmsbjl='" + BMSBJL + "',zt='" + STATE + "',lcid='" + requestid + "' where gjahr='" + GJAHR + "' and monat='" + MONAT + "' and bmbm='" + BMBM + "'  and werks='" + WERKS + "'";
                    rs1.execute(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }

            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;

        } else {
            try {
                //主表
                String BMBM1 = "00000000";
                String BMBM_T = Util.null2String(rs1.getString("BMBM_T"));//部门编码名称
                String BMSBJBXY = Util.null2String(rs1.getString("BMSBJBXY"));//部门上报加班效益工资
                String BMSBJBTC = Util.null2String(rs1.getString("BMSBJBTC"));//部门上报加班提成工资
                String BMSBSK = Util.null2String(rs1.getString("BMSBSK"));//部门上报收款提成
                String BMSBZZSKJ = Util.null2String(rs1.getString("BMSBZZSKJ"));//部门上报自助收款机提成
                String BMSBJL = Util.null2String(rs1.getString("BMSBJL"));//部门上报激励奖金
                String BMGDJBXY = Util.null2String(rs1.getString("BMGDJBXY"));//部门规定加班效益工资
                String BMGDJBTC = Util.null2String(rs1.getString("BMGDJBTC"));//部门规定加班提成工资
                String BMGDSK1 = Util.null2String(rs1.getString("BMGDSK"));//部门规定收款提成
                String BMGDZZSKJ = Util.null2String(rs1.getString("BMGDZZSKJ"));//部门规定自助收款机提成
                String BMGDJL = Util.null2String(rs1.getString("BMGDJL"));//部门规定激励奖金
                Map<String, String> map1 = new HashMap();
                map1.put("NUMID", NUMID1);
                map1.put("GJAHR", GJAHR);
                map1.put("MONAT", MONAT);
                map1.put("BMBM", BMBM1);
                map1.put("BMBM_T", BMBM_T);
                map1.put("BMSBJBXY", BMSBJBXY);
                map1.put("BMSBJBTC", BMSBJBTC);
                map1.put("BMSBSK", BMSBSK);
                map1.put("BMSBZZSKJ", BMSBZZSKJ);
                map1.put("BMSBJL", BMSBJL);
                map1.put("BMGDJBXY", BMGDJBXY);
                map1.put("BMGDJBTC", BMGDJBTC);
                map1.put("BMGDSK", BMGDSK1);
                map1.put("BMGDZZSKJ", BMGDZZSKJ);
                map1.put("BMGDJL", BMGDJL);
                baseBean.writeLog("else汇总表参数:" + map1.toString());
                //汇总表明细
                String sqlhz = "select d.* from uf_jxxfhz t left join uf_jxxfhz_dt1 d on t.id=d.mainid and t.numid = d.numid  where d.numid='" + NUMID1 + "'";
                rs3.executeQuery(sqlhz);
                baseBean.writeLog(sqlhz);
                while (rs3.next()) {
                    String GZ = Util.null2String(rs3.getString("GZ"));
                    String GZ_T = Util.null2String(rs3.getString("GZ_T"));
                    String XYJE = Util.null2String(rs3.getString("XYJE"));
                    Map<String, String> map3 = new HashMap<String, String>();
                    map3.put("NUMID", NUMID1);
                    map3.put("GZ", GZ);
                    map3.put("GZ_T", GZ_T);
                    map3.put("XYJE", XYJE);
                    baseBean.writeLog("汇总表明细参数:" + map3.toString());
                    list.add(map3);
                }
                baseBean.writeLog("list相关参数:" + list.toString());
                //明细表
                String sqlmx = "select numid,pernr,jbxygz,jbtcgz,bmgdsk,bmgdsk1,zzskjtc,jljj  from " + tablename + "_dt1 where mainid='" + mainid + "' and werks='" + WERKS + "' and gjahr='" + GJAHR + "' and monat='" + MONAT + "'";
                rs2.executeQuery(sqlmx);
                while (rs2.next()) {
                    String NUMID = Util.null2String(rs2.getString("NUMID"));         //唯一标识
                    String PERNR = Util.null2String(rs2.getString("PERNR"));       //人员编码
                    String JBXYGZ = Util.null2String(rs2.getString("JBXYGZ"));         //加班效益工资
                    String JBTCGZ = Util.null2String(rs2.getString("JBTCGZ"));         //加班提成工资
                    String BMGDSK = Util.null2String(rs2.getString("BMGDSK"));         //部门规定收款提成
                    String SKTC = Util.null2String(rs2.getString("BMGDSK1"));         //收款提成
                    String ZZSKJTC = Util.null2String(rs2.getString("ZZSKJTC"));         //自助收款机提成
                    String JLJJ = Util.null2String(rs2.getString("JLJJ"));             //激励奖金
                    Map<String, String> map2 = new HashMap();
                    map2.put("GJAHR", GJAHR);
                    map2.put("MONAT", MONAT);
                    map2.put("PERNR", PERNR);
                    map2.put("JBXYGZ", JBXYGZ);
                    map2.put("JBTCGZ", JBTCGZ);
                    map2.put("BMGDSK", BMGDSK);
                    map2.put("SKTC", SKTC);
                    map2.put("ZZSKJTC", ZZSKJTC);
                    map2.put("JLJJ", JLJJ);
                    list1.add(map2);
                    baseBean.writeLog("明细表相关参数:" + map2.toString());
                }
                //调用接口
                Map resultMap = hr031B.execute(list1);
                String flag2 = (String) resultMap.get("flag");
                String message2 = (String) resultMap.get("message");
                baseBean.writeLog("ZFM_HR_031B接口返回结果——flag:" + flag2 + ";message:" + message2);
                if ("s".equalsIgnoreCase(flag2)) {
                    rs2.executeQuery(sqlmx);
                    while (rs2.next()) {
                        String NUMID = Util.null2String(rs2.getString("NUMID"));         //唯一标识
                        String JBXYGZ = Util.null2String(rs2.getString("JBXYGZ"));         //加班效益工资
                        String JBTCGZ = Util.null2String(rs2.getString("JBTCGZ"));         //加班提成工资
                        String BMGDSK = Util.null2String(rs2.getString("BMGDSK"));         //部门收款提成
                        String SKTC = Util.null2String(rs2.getString("BMGDSK1"));         //收款提成
                        String ZZSKJTC = Util.null2String(rs2.getString("ZZSKJTC"));       //自助收款机提成
                        String JLJJ = Util.null2String(rs2.getString("JLJJ"));             //激励奖金
                        String updateSQL2 = "update uf_jxxfmx set jbxygz='" + JBXYGZ + "' ,jbtcgz= '" + JBTCGZ + "', bmgdsk='" + BMGDSK + "',bmgdsk1='"+SKTC+"', zzskjtc='" + ZZSKJTC + "', jljj='" + JLJJ + "' where numid='" + NUMID + "'";
                        rs2.execute(updateSQL2);
                        baseBean.writeLog(updateSQL2);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message2);
                    return Action.FAILURE_AND_CONTINUE;
                }
                //调用接口
                Map resultMap1 = hr_031A.execute(map1,list);
                String flag1 = (String) resultMap1.get("flag");
                String message1 = (String) resultMap1.get("message");
                baseBean.writeLog("SAP返回结果——flag:" + flag1 + ";message:" + message1);
                if ("s".equalsIgnoreCase(flag1)) {
                    String updateSQL1 = "update uf_jxxfhz set bmsbjbxy='" + BMSBJBXY + "', bmsbjbtc='" + BMSBJBTC + "', bmsbsk='" +
                            BMSBSK + "',bmsbzzskj='" + BMSBZZSKJ + "',bmsbjl='" + BMSBJL + "',zt='" + STATE + "',lcid='" + requestid + "' where gjahr='" + GJAHR + "' and monat='" + MONAT + "' and werks='" + WERKS + "'";
                    rs1.execute(updateSQL1);
                    baseBean.writeLog("执行语句：" + updateSQL1);
                } else {
                    requestInfo.getRequestManager().setMessageid("111100");
                    requestInfo.getRequestManager().setMessagecontent(message1);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } catch (Exception e) {
                e.printStackTrace();
                baseBean.writeLog("异常参考:" + e);
                requestInfo.getRequestManager().setMessageid("111100");
                requestInfo.getRequestManager().setMessagecontent(e.getMessage());
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        }
    }
}

